<?php
/**
 +------------------------------------------------------------------------------
 * AssetsreportAction控制器类
 +------------------------------------------------------------------------------
 * @category   资产报表
 * @package  app
 * @subpackage  Action
 * @author   朝闻道 <tb>
 * @date 2014.03.17
 * @time  下午2:44:22
 +------------------------------------------------------------------------------
 */
class GivereportAction extends BaseAction {
	protected $dao;
	
	function _initialize() {
		parent::_initialize ();
		$this->dao = M("UserGive");
	}
	
	/**
	 * 分期总统计报表
	 * @author tb
	 * @date 2014.03.25
	 * */
	function give(){
		$oedustr = array(
			'countoedu' => '总分期',
			'overoedu' => '已到帐',
			'notoedu_yes' => '未到帐（有效）',
			'notoedu_no' => '未到帐（无效）'
		);
		
		$where['moneytype'] = 'CV';
		$list['countoedu'] = round($this->dao->where($where)->sum('countoedu/100'),2);
		$list['overoedu'] = round($this->dao->where($where)->sum('overoedu/100'),2);
		$where['status'] = 1;
		$list['notoedu_yes'] = round($this->dao->where($where)->sum('notoveroedu/100'),2);
		$where['status'] = 0;
		$list['notoedu_no'] = round($this->dao->where($where)->sum('notoveroedu/100'),2);

		$this->assign('count',count($list));
		$this->assign("oedustr",$oedustr);
		$this->assign("list",$list);
		
		/**月查询**/
		$listmonth = self::givemonth(0);
		$this->assign('listmonth',$listmonth);
		/**月查询**/
		
		/**年查询**/
		$listyear = self::giveyear(0);
		$this->assign('totalyear',array_sum($listyear));
		$this->assign('listyear',$listyear);
		/**年查询**/
			
		$this->display();
	}
	
	/**
	 * 分期年统计报表
	 * @author tb
	 * @date 2014.03.25
	 * */
	function giveyear($DISPLAY = 1){
		//当年统计
		$date = array(
			//'year' => '%Y',
			'month' => '%Y%m',
			'day' => '%Y%m%d',
			'week' => '%Y%m%d'
		);
		
		foreach ($date as $key=>$d){
		
			$where = " = DATE_FORMAT(now(),'$d') ";
			if($key == 'week'){
				$where = " BETWEEN DATE_FORMAT(now(),'$d') - WEEKDAY(now()) and  ( DATE_FORMAT(now(),'$d') - WEEKDAY(now()))+6 ";
			}
			
			$sql = "select DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(updatetime,'%Y%m%d'), INTERVAL 30 DAY),'$d') date,SUM(eachoedu) value
	from user_give 
	 where moneytype='CV' and  DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(updatetime,'%Y%m%d'), INTERVAL 30 DAY),'$d') $where  
	GROUP BY DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(updatetime,'%Y%m%d'), INTERVAL 30 DAY),'%Y')";
			
			$res = $this->dao->query($sql);//echo $sql;echo  '<br />';
			$list[$key] = round($res[0]['value']/100,2);
		}
		
		$time = array(
			'day' => array('btime'=>gettime(date('Y-m-d')." 00:00:00"),'etime'=>gettime(date('Y-m-d')." 23:59:59")),
			'week' => array('btime'=>gettime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1,date("Y")))),'etime'=>gettime(date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7,date("Y"))))),
			'month' => array('btime'=>gettime(date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")))),'etime'=>gettime(date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y"))))),
			'year' => array('btime'=>gettime(date("Y")."-01-01 00:00:00"),'etime'=>gettime(date('Y')."-12-30 23:59:59"))
		);
		$this->assign('time',$time);
		
		$listmonth = self::givemonth(0);
		//年到账
		foreach ($listmonth[date('Y')] as $value){
			$list['year'] += $value;
		}
	
		if(!$DISPLAY){
			return $list;
		}else{		
			$this->assign('total',array_sum($list));
			$this->assign('list',$list);		
			$this->display();
		}
	}
	
	
	/**
	 * 分期月统计报表
	 * @author tb
	 * @date 2014.03.25
	 * */
	function givemonth($DISPLAY = 1){
		$cyear = intval(date('Y'));
		$sql = "select FROM_UNIXTIME(max(endday),'%Y') as y from user_give ORDER BY endday desc";
		$maxyear = $this->dao->query($sql);
		$maxyear = intval($maxyear[0]['y']);
		$i = 0 ;
		for ($year = $cyear; $year<=$maxyear; $year++){
			
			if($year == $cyear){
				$month = intval(date('m'));
			}else{
				$month = 1;
			}
			
			if(strlen($year)==1){
				$y = '0'.$year;
			}else{
				$y = $year;
			}
			
			for($month; $month<=12; $month++){
				$i++;
				if(strlen($month)==1){
					$m = '0'.$month;
				}else{
					$m = $month;
				}
				
				$date = $y.$m;
				
				$sql = "select DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(updatetime,'%Y%m%d'), INTERVAL 30*$i DAY),'%Y%m') date,SUM(eachoedu) eachoedu  from user_give 
 where  moneytype='CV' and  DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(updatetime,'%Y%m%d'), INTERVAL 30*$i DAY),'%Y%m') = '$date'  
GROUP BY DATE_FORMAT(DATE_ADD(FROM_UNIXTIME(updatetime,'%Y%m%d'), INTERVAL 30*$i DAY),'%Y%m') HAVING FROM_UNIXTIME(max(endday),'%Y%m') >= $date";
				
				if($date == '201901'){
					echo $sql;
				}
				
				$res = $this->dao->query($sql);
				$list[$y][$m] = round($res[0]['eachoedu']/100,2);
			}
			
		}
		
		if(!$DISPLAY){
			return $list;
		}else{
			$this->assign('list',$list);
			$this->display();
		}

	
	}



	/**
	 * 饼状图形报表
	 * @author tb
	 * @date 2014.03.20
	 * */
	function piechart(){
		$changtype = array();$subject= array();
		$list = array();
		if ( $_REQUEST['todo'] == 'piechart' ) {
			$list = self::chart();
			
			foreach ($list as $vo){
				$pieData .= abs($vo['value']).",";
				$pieLegend[] = "%%.%%%% – ".$vo['subject']." - ".$vo['value']." - ".$this->changt_str[$vo['changtype']];
			}
	
			$pieData = substr($pieData, 0,strlen($pieData)-1);
			$pieLegend =json_encode($pieLegend);
				
			//$pieData = "55973.91,79846746.33,16981,44610";
			$this->assign("pieData",$pieData);
			$this->assign("pieLegend",$pieLegend);
			$this->display();
		}else{
			$re['ids'] = implode(',',$_REQUEST['ids']);
			$re['ftodo'] = $_REQUEST['ftodo'];
			$re['action'] = 'piechart';
			die( json_encode( $re ) );
			//$this->display();
		}
	
	}

	/**
	 * 柱状图形报表
	 * @author tb
	 * @date 2014.03.22
	 * */
	function barchart(){
		$changtype = array();$subject= array();
		if ( $_REQUEST['todo'] == 'barchart' ) {
			//设置图形 宽度 
			$barwidth = empty($_REQUEST['barwidth']) ? 400 : $_REQUEST['barwidth'];
			$list = self::chart();
			foreach ($list as $vo){
				$barData[0][]  = abs($vo['value']);
				$pieLabel[0][] = $this->changt_str[$vo['changtype']]." ".$vo['subject'];
			}
			$barData = empty($barData) ? array(array(0)) : $barData; //柱状数据
			$pieLabel = empty($pieLabel) ? array(array('无')) : $pieLabel; //柱状显示文本
			$barData =json_encode($barData);
			$pieLabel =json_encode($pieLabel);			
			$this->assign("barData",$barData);
			$this->assign("pieLabel",$pieLabel);
			$this->assign("barwidth",$barwidth);
			$this->display();
		}else{
			$re['ids'] = implode(',',$_REQUEST['ids']);
			$re['ftodo'] = $_REQUEST['ftodo'];
			$re['action'] = 'barchart';
			die( json_encode( $re ) );
			//$this->display();
		}
	}
	
	
	/**
	 * 选择科目的统计列表
	 * @author tb
	 * @date 2014.03.22
	 * */
	function chart(){
		$ids = $_REQUEST['ids'] ;
		$ids = explode(',',$ids);		
		$list = array();
		foreach ($ids as $str){
			$res = $arr = array();		
			$arr = explode('|', $str);
			if($arr[0] == "coupon"){
				$coupon = self::coupon_total();
				$coupon_data['subject'] = 'CV';
				if($arr[1] == 'xfcz'){
					$coupon_data['changtype'] = 'couponxfcz';
					$coupon_data['value'] = $coupon['total']/100;
							
				}elseif($arr[1] == 'xf'){
					$coupon_data['changtype'] = 'couponxf';
					$coupon_data['value'] = $coupon['use']/100;
				}
			    $res[] = $coupon_data;
				$list = array_merge($list,$res);
			}else{		
				$changtype = "'".$arr[0]."'";
				$subject = "'".$arr[1]."'";
				$res = self::asset_report($changtype,$subject);
				$list = array_merge($list,$res);
			}				
			
				
		}
		return  $list;
	}
	
	
}//end AssetsreportAction